home *** CD-ROM | disk | FTP | other *** search
/ ftp.qualcomm.com / 2014.06.ftp.qualcomm.com.tar / ftp.qualcomm.com / eudora / developers / emsapi / carbon_emsapi.sit.hqx / Macintosh API Support / copycat.c < prev    next >
Text File  |  2001-03-08  |  4KB  |  190 lines

  1. /* ======================================================================
  2.  
  3.     Functions to manage string buffers
  4.     This supports:
  5.         Pascal Strings - one length byte plus string
  6.         C Strings      - Null terminated string in buffer
  7.         Buffers        - String data and a length
  8.  
  9.     These mostly return the length of the result
  10.  
  11.     Filename:            copycat.c
  12.     Last Edited:        March 7, 1997
  13.     Authors:            Laurence Lundblade, Myra Callen
  14.     Copyright:            1995, 1996 QUALCOMM Inc.
  15.     Technical support:    <emsapi-info@qualcomm.com>
  16. */
  17.  
  18. #include <ctype.h>
  19. #include "copycat.h"
  20.  
  21.  
  22. /* Copy a pascal string into a buffer, return length */
  23. long CopyPB(const StringPtr source, char *dest)
  24. {
  25.     BlockMoveData(source + 1, dest, *source);
  26.     return (*source);
  27. }
  28.  
  29. /* Copy a buffer to a Pascal string, return length */
  30. long CopyBP(const char *source, long sourceLen, StringPtr dest)
  31. {
  32.     BlockMoveData(source, dest + 1, sourceLen);
  33.     *dest = sourceLen;
  34.     return sourceLen;
  35. }
  36.  
  37. /* Make a copy of a Pascal string, return length */
  38. long CopyPP(const StringPtr source, StringPtr dest)
  39. {
  40.     BlockMoveData(source+1, dest + 1, *source);
  41.     *dest = *source;
  42.     return (*dest);
  43. }
  44.  
  45. /* Copy a Pascal string into a C string, return length */
  46. long CopyPC(const StringPtr source, char *dest)
  47. {
  48.     BlockMoveData(source+1, dest, *source);
  49.     *(dest + *source) = '\0';
  50.     return (*source);
  51. }
  52.  
  53. /* Copy C string into a Pascal string, return length */
  54. long CopyCP(const char *source, StringPtr dest)
  55. {
  56.     char *s, *d;
  57.  
  58.     s = (char*) source;
  59.     for (d = (char*) dest + 1; *s; *d++ = *s++)
  60.         ;
  61.     *dest = s - source;
  62.     return (*dest);
  63. }
  64.  
  65. #pragma mark -
  66.  
  67. /* Append a pascal string into a buffer, return length */
  68. long CatPB(const StringPtr addition, long destLen, char *dest)
  69. {
  70.     BlockMoveData(addition + 1, dest + destLen, *addition);
  71.     destLen += *addition;
  72.     return destLen;
  73. }
  74.  
  75. /* Append a buffer to a Pascal string, return length */
  76. long CatBP(const char *addition, long additionLen, StringPtr dest)
  77. {
  78.     BlockMoveData(addition, dest + 1 + *dest, additionLen);
  79.     *dest += additionLen;
  80.     return (*dest);
  81. }
  82.  
  83. /* Append a Pascal string to another, return length */
  84. long CatPP(const StringPtr addition, StringPtr dest)
  85. {
  86.     BlockMoveData(addition + 1,  dest + 1 + *dest, *addition);
  87.     *dest += *addition;
  88.     return (*dest);
  89. }
  90.  
  91. #pragma mark -
  92.  
  93. /* ----------------------------------------------------------------------
  94.     A case insensitive strncmp() like thing
  95.  
  96.     Args:    o - First string to compare (null terminated)
  97.             r - Second buffer (string) to compare
  98.             n - length of second string
  99.             caseignore
  100.  
  101.     Result: integer indicating which is greater
  102.  */
  103. short CmpCB(const char *o, const char *r, long n, short caseignore)
  104. {
  105.     if (r == nil && o == nil)
  106.         return 0;
  107.     if (o == nil)
  108.         return 1;
  109.     if (r == nil)
  110.         return -1;
  111.  
  112.     n--;
  113.     while (n && *o && (caseignore ?
  114.             ((isupper(*o) ? tolower(*o) : *o) == (isupper(*r) ? tolower(*r) : *r)) :
  115.             (*o == *r))) {
  116.         o++;
  117.         r++;
  118.         n--;
  119.     }
  120.     return (caseignore ? ((isupper(*o) ? tolower(*o) : *o) -
  121.                           (isupper(*r) ? tolower(*r) : *r)) : (*o - *r));
  122. }
  123.  
  124. /* ----------------------------------------------------------------------
  125.     A case insensitive strncmp() like thing
  126.  
  127.     Args:    o - First string to compare - a PASCAL string
  128.             r - Second buffer (string) to compare
  129.             n - length of second string
  130.             caseignore
  131.  
  132.     Result: integer indicating which is greater
  133.  */
  134. short CmpPB(const StringPtr o, const char *r, long n, short caseignore)
  135. {
  136.     char    *oc, *o_end;
  137.  
  138.     if (r == nil && o == nil)
  139.         return 0;
  140.     if (o == nil)
  141.         return 1;
  142.     if (r == nil)
  143.         return -1;
  144.  
  145.     oc = (char*) o;
  146.      o_end = (char*) oc + (*oc + 1);
  147.     n--;
  148.     oc++;
  149.     while (n && (oc < o_end) &&
  150.                 (caseignore ? ((isupper(*oc) ? tolower(*oc) : *oc) ==
  151.                 (isupper(*r) ? tolower(*r) : *r)) : (*oc == *r))) {
  152.         oc++;
  153.         r++;
  154.         n--;
  155.     }
  156.     return (caseignore ? ((isupper(*oc) ? tolower(*oc) : *oc) -
  157.                           (isupper(*r) ? tolower(*r) : *r)) : (*oc - *r));
  158. }
  159.  
  160. #pragma mark -
  161.  
  162. int stricmp(const char *s1, const char *s2)
  163. {
  164.     char c1, c2;
  165.     while (1)
  166.     {
  167.         c1 = tolower(*s1++);
  168.         c2 = tolower(*s2++);
  169.         if (c1 < c2) return -1;
  170.         if (c1 > c2) return 1;
  171.         if (c1 == 0) return 0;
  172.     }
  173. }
  174.  
  175.  
  176. int strnicmp(const char *s1, const char *s2, int n)
  177. {
  178.     int i;
  179.     char c1, c2;
  180.     for (i=0; i<n; i++)
  181.     {
  182.         c1 = tolower(*s1++);
  183.         c2 = tolower(*s2++);
  184.         if (c1 < c2) return -1;
  185.         if (c1 > c2) return 1;
  186.         if (!c1) return 0;
  187.     }
  188.     return 0;
  189. }
  190.